package com.ruoyi.reception;

import com.aliyun.oss.OSSClient;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @author chenjian
 * @version 1.0.0
 * @ClassName UploadController.java
 * @Description oos Upload controller
 * @createTime 2020年08月13日 14:26:00
 */
@RestController
@CrossOrigin
public class UploadController {

    @Value("${oss.accessKeyId}")
    private String accessKeyId;

    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${oss.bucketName}")
    private String bucketName;

    @Value("${oss.endPoint}")
    private  String  endPoint ;

    @RequestMapping("/reception/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) {
        try {
            //获取当前日期
            SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            java.util.Date date = new java.util.Date();
            String today = simpleDateFormat.format(date);;

            // 获取文件后缀
            String  prefix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS",Locale.CHINA);
            String dateStr = sdf.format(new Date());

            String fileName =dateStr + prefix;

            //https://cloud-biz.oss-cn-qingdao.aliyuncs.com/2019-11-06/20191106145715856.png
            String cloudPath = "https://"+bucketName+"."+endPoint+"/"+today+"/"+fileName;

            // 用uuid作为文件名，防止生成的临时文件重复
            final File excelFile = File.createTempFile(today, prefix);

            // MultipartFile to File
            file.transferTo(excelFile);

            //上传文件到OOS
            toOOS(excelFile,today+"/"+fileName);

            Map param = new HashMap();
            param.put("cloudPath", cloudPath);
            //param.put("nginx_path", today + "/" + fileName);
            //param.put("upload_user", "current user");
            //param.put("orig_file_name", file.getOriginalFilename());
            //param.put("file_name", fileName);

            System.out.println("cloudPath==>"+cloudPath);
            return AjaxResult.success(param);
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("上传失败"+e.toString());
        }
    }

    private void toOOS(File file, String fileName) {

        // 创建ObsClient实例
        OSSClient ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);

        ossClient.putObject(bucketName, fileName, file);

        try {
            // 关闭ossClient
            ossClient.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}